<template>
  <div class="container">
    <!-- 路径导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>内容管理</el-breadcrumb-item>
      <el-breadcrumb-item>心理答疑管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="问题名称">
          <el-input v-model="queryInfo.name" clearable @clear="queryList" placeholder="请输入问题名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="queryList">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-row>


    <!-- 表格区域 -->
    <el-table :data="questionList" border style="width: 100%" :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}">
      <el-table-column width="80px" prop="id" label="ID" />
      <el-table-column prop="title" label="问题标题" />
      <el-table-column prop="content" label="问题内容" show-overflow-tooltip />
      <el-table-column prop="createTime" label="提问时间" />
      <el-table-column prop="userId" label="提问人" #default="scope">
        <el-tag>{{ getUserName(scope.row.userId) }}</el-tag>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button icon="el-icon-edit" type="primary" size="mini" @click="updateQuestion(scope.row)">编辑</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteQuestion(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 15, 20]" :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 新增和修改的对话框 -->
    <el-dialog title="修改问题" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="question">
        <el-form-item label="问题标题" label-width="80px">
          <el-input placeholder="请输入问题标题" v-model="question.title"></el-input>
        </el-form-item>
        <el-form-item label="问题内容" label-width="80px">
          <el-input placeholder="请输入问题内容" v-model="question.content"></el-input>
        </el-form-item>
        <el-form-item label="提问时间" label-width="80px">
          <el-date-picker v-model="question.createTime" type="datetime" placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提问人" label-width="80px">
          <el-select v-model="question.userId" placeholder="请选择">
            <el-option v-for="item in userList" :key="item.id" :label="item.username" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import {url} from '@/util/tools.js'

export default {
  data() {
    return {
      url: '',
      // 查询信息对象
      queryInfo: {
        title: '',
        pageNum: 1,
        pageSize: 5
      },
      total: 0,
      questionList: [],

      dialogFormVisible: false, // 是否显示对话框
      question: {
        id: '',
        title: '',
        content: '',
        createTime: '',
        userId: ''
      },
      dialogVisible: false,
      userList: []

    }
  },
  methods: {


    // 添加/修改心理知识
    add() {
      this.$http({
        method: "PUT",
        url: 'question/update',
        data: this.question
      }).then(res => {
        if (res.data.status === 200) {
          this.$message.success("更新成功")
          this.questionIsNull()
          this.dialogFormVisible = false
          this.queryList()
        } else {
          this.$message.error("更新失败")
        }
      })
    },
    cancle() {
      this.dialogFormVisible = false
      this.questionIsNull()
      this.queryList()
    },


    updateQuestion(row) {
      this.question = row
      this.dialogFormVisible = true
    },


    deleteQuestion(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then().then(() => {
        this.$http.delete('question/delete/' + id).then((resp) => {
          if (resp.data.status == 200) {
            this.$message.success(resp.data.message);
            this.queryInfo.pageNum = 1
            this.queryList();
          } else {
            this.$message.error(resp.data.message);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },


    queryUserList() {
      this.$http.get('user/getIdAndUsername').then(res => {
        // console.log(res)
        if (res.data.status == 200) {
          console.log(res)
          this.userList = res.data.data
        }
      })
    },


    queryList() {
      if (this.queryInfo.title !== '') {
        this.queryInfo.pageNum = 1
      }
      this.$http.get('question/page', {
        params: this.queryInfo
      }).then(res => {
        if (res.data.status == 200) {
          this.questionList = res.data.data.list;
          this.total = res.data.data.total
          // console.log(res)
        } else {
          this.$message.error('查询失败!')
        }
      })
    },
    // 设置heartknowledge为Null
    questionIsNull() {
      this.question = {
        id: '',
        title: '',
        content: '',
        createTime: '',
        userId: ''
      }
    },
    getUserName(userId) {
      // console.log(typeId)
      for (let i = 0; i < this.userList.length; i++) {
        if (this.userList[i].id == userId) {
          // console.log(this.typeList[i].typeName)
          return this.userList[i].username
        }
      }
    },
    handleSizeChange(size) {
      // console.log('每页显示数量发生改变');
      this.queryInfo.pageSize = size
      this.queryInfo.pageNum = 1
      this.queryList()
    },
    handleCurrentChange(num) {
      // console.log('页码发生改变');
      this.queryInfo.pageNum = num
      this.queryList()
    }
  },
  mounted() {
    this.queryList();
    this.queryUserList()
    // 动态的获取tool.js中的url，并赋值给当前组件的全局url
  }
}
</script>
<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}

.el-pagination {
  margin-top: 15px;
}

.el-link {
  padding: 0 5px;
}

/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
.ql-container {
  min-height: 200px;
}
</style>
